package com.hans.myscanner.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.hans.myscanner.R;

public class FooterNavBar extends LinearLayout {
    private Context context;
    private Drawable firstItemIconDrawable;
    private Drawable secondItemIconDrawable;
    private Drawable thirdItemIconDrawable;
    private Drawable fourthItemIconDrawable;
    private String firstItemText,secondItemText,thirdItemText,fourthItemText;
    private int iconTextPadding,itemTextColor;
    private float itemTextSize;
    private String textFontPathInAssets;
    private CustomFontTextView[] items;

    private CustomFontTextView firstItem,secondItem,thirdItem,fourthItem;

    public FooterNavBar(Context context) {
        this(context,null);
    }

    public FooterNavBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FooterNavBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        findView();
        initView();
    }

    private void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FooterNavBar);
        firstItemIconDrawable = ta.getDrawable(R.styleable.FooterNavBar_firstItemIconDrawable);
        secondItemIconDrawable = ta.getDrawable(R.styleable.FooterNavBar_secondItemIconDrawable);
        thirdItemIconDrawable = ta.getDrawable(R.styleable.FooterNavBar_thirdItemIconDrawable);
        fourthItemIconDrawable = ta.getDrawable(R.styleable.FooterNavBar_fourthItemIconDrawable);
        firstItemText = ta.getString(R.styleable.FooterNavBar_firstItemText);
        secondItemText = ta.getString(R.styleable.FooterNavBar_secondItemText);
        thirdItemText = ta.getString(R.styleable.FooterNavBar_thirdItemText);
        fourthItemText = ta.getString(R.styleable.FooterNavBar_fourthItemText);
        iconTextPadding = ta.getDimensionPixelSize(R.styleable.FooterNavBar_iconTextPadding,0);
        itemTextSize = ta.getDimension(R.styleable.FooterNavBar_itemTextSize,0f);
        itemTextColor = ta.getColor(R.styleable.FooterNavBar_itemTextColor, Color.WHITE);
        textFontPathInAssets = ta.getString(R.styleable.FooterNavBar_itemTextFontPathInAssets);
        ta.recycle();
    }

    private void findView(){
        View view = LayoutInflater.from(context).inflate(R.layout.widget_footer_nav_bar,this);
        firstItem = (CustomFontTextView) view.findViewById(R.id.nav_tab_1);
        secondItem = (CustomFontTextView) view.findViewById(R.id.nav_tab_2);
        thirdItem = (CustomFontTextView) view.findViewById(R.id.nav_tab_3);
        fourthItem = (CustomFontTextView) view.findViewById(R.id.nav_tab_4);
        items = new CustomFontTextView[]{firstItem,secondItem,thirdItem,fourthItem};
    }

    private void initView(){
        if(textFontPathInAssets != null){
            try {
                Typeface tf = Typeface.createFromAsset(context.getAssets(),textFontPathInAssets);
                firstItem.setTypeface(tf);
                secondItem.setTypeface(tf);
                thirdItem.setTypeface(tf);
                fourthItem.setTypeface(tf);
            }catch (Exception e){}

        }

        firstItemIconDrawable.setBounds(0,0,firstItemIconDrawable.getMinimumWidth(),firstItemIconDrawable.getMinimumHeight());
        secondItemIconDrawable.setBounds(0,0,secondItemIconDrawable.getMinimumWidth(),secondItemIconDrawable.getMinimumHeight());
        thirdItemIconDrawable.setBounds(0,0,thirdItemIconDrawable.getMinimumWidth(),thirdItemIconDrawable.getMinimumHeight());
        fourthItemIconDrawable.setBounds(0,0,fourthItemIconDrawable.getMinimumWidth(),fourthItemIconDrawable.getMinimumHeight());

        firstItem.setCompoundDrawables(null,firstItemIconDrawable,null,null);
        secondItem.setCompoundDrawables(null,secondItemIconDrawable,null,null);
        thirdItem.setCompoundDrawables(null,thirdItemIconDrawable,null,null);
        fourthItem.setCompoundDrawables(null,fourthItemIconDrawable,null,null);

        firstItem.setCompoundDrawablePadding(iconTextPadding);
        secondItem.setCompoundDrawablePadding(iconTextPadding);
        thirdItem.setCompoundDrawablePadding(iconTextPadding);
        fourthItem.setCompoundDrawablePadding(iconTextPadding);
        firstItem.setText(firstItemText);
        secondItem.setText(secondItemText);
        thirdItem.setText(thirdItemText);
        fourthItem.setText(fourthItemText);
        firstItem.setTextSize(itemTextSize);
        secondItem.setTextSize(itemTextSize);
        thirdItem.setTextSize(itemTextSize);
        fourthItem.setTextSize(itemTextSize);
        firstItem.setTextColor(itemTextColor);
        secondItem.setTextColor(itemTextColor);
        thirdItem.setTextColor(itemTextColor);
        fourthItem.setTextColor(itemTextColor);

        firstItem.setOnClickListener(onClickListener);
        secondItem.setOnClickListener(onClickListener);
        thirdItem.setOnClickListener(onClickListener);
        fourthItem.setOnClickListener(onClickListener);
    }
    private int currentPosition = -1;
    public void setCurrentPosition(int position){
        if(currentPosition == position) return;
        if(onNavItemChangedListener == null) throw new NullPointerException("请先调用setOnNavItemChangedListener设置回调");
        switch (position){
            case 0:
                onNavItemChangedListener.onNavItemChanged(items, firstItem, 0);
                break;
            case 1:
                onNavItemChangedListener.onNavItemChanged(items, secondItem, 1);
                break;
            case 2:
                onNavItemChangedListener.onNavItemChanged(items, thirdItem, 2);
                break;
            case 3:
                onNavItemChangedListener.onNavItemChanged(items, fourthItem, 3);
                break;
        }
        currentPosition = position;
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(onNavItemChangedListener == null) return;
            int i = v.getId();
            if (i == R.id.nav_tab_1) {
               setCurrentPosition(0);
            } else if (i == R.id.nav_tab_2) {
                setCurrentPosition(1);
            } else if (i == R.id.nav_tab_3) {
                setCurrentPosition(2);
            } else if (i == R.id.nav_tab_4) {
                setCurrentPosition(3);
            }
        }
    };

    private OnNavItemChangedListener onNavItemChangedListener;

    public interface OnNavItemChangedListener{
        void onNavItemChanged(CustomFontTextView[] items, CustomFontTextView item, int posistion);
    }

    public void setOnNavItemChangedListener(OnNavItemChangedListener onNavItemChangedListener) {
        this.onNavItemChangedListener = onNavItemChangedListener;
    }
}
